package cn.xbtop.blog.service.impl;

import cn.xbtop.blog.mapper.BlogMapper;
import cn.xbtop.blog.mapper.TypeMapper;
import cn.xbtop.blog.pojo.Blog;
import cn.xbtop.blog.pojo.Type;
import cn.xbtop.blog.service.BlogService;
import cn.xbtop.blog.utils.PageBean;
import cn.xbtop.blog.utils.UUIDUtils;
import cn.xbtop.blog.vo.BlogVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/2/19 15:46
 */
@Service
public class BlogServiceImpl implements BlogService {

    @Resource
    private BlogMapper blogMapper;
    @Autowired
    private TypeMapper typeMapper;

    /**
     * 根据id删除
     * @param blogId
     * @return
     */
    @Override
    public int deleteByPrimaryKey(String blogId) {
        return blogMapper.deleteByPrimaryKey(blogId);
    }

    /**
     * 添加
     *
     * @param blog
     * @return
     */
    @Override
    public int insert(Blog blog) {
        //生成id
        blog.setBlogId(UUIDUtils.getUUID());
        //设置日期
        blog.setCreatedTime(new Date());
        return blogMapper.insert(blog);
    }

    /**
     * 根据id查询
     * @param blogId
     * @return
     */
    @Override
    public Blog selectByPrimaryKey(String blogId) {
        return blogMapper.selectByPrimaryKey(blogId);
    }

    /**
     * 更新
     * @param blog
     * @return
     */
    @Override
    public int updateByPrimaryKey(Blog blog) {
        return blogMapper.updateByPrimaryKey(blog);
    }

    /**
     * 查询所有博客
     *
     * @return
     */
    @Override
    public List<Blog> findAllBlog() {
        return blogMapper.findAllBlog();
    }

    /**
     * 阅读方法
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BlogVo readById(String id) {
        //阅读的话，需要去更新阅读数
        Blog blog = blogMapper.selectByPrimaryKey(id);
        //阅读数+1
        blog.setBlogRead(blog.getBlogRead() + 1);
        //更新阅读数+1
        blogMapper.updateByPrimaryKey(blog);
        //将 blog 转为 blogVo
        BlogVo blogVo = new BlogVo();
        //使用 spring的
        BeanUtils.copyProperties(blog, blogVo);
        //查询分类
        Type type = typeMapper.selectByPrimaryKey(blog.getBlogType());
        blogVo.setTypeName(type.getTypeName());
        //设置更新时间
        blogVo.setUpdateTime(new Date());
        return blogVo;
    }

    /**
     * 分页查询所有
     * @param pageBean
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageBean<BlogVo> findByPage(PageBean<BlogVo> pageBean) {
        //查询数据
        List<BlogVo> list =  blogMapper.findByPage(pageBean);
        pageBean.setList(list);
        //查询总数
        int totalCount = blogMapper.getCountByPage(pageBean);

        pageBean.setTotalCount(totalCount);
        return pageBean;
    }
}
